import os
import subprocess
import time
import requests
from pip._internal.metadata import pkg_resources

from config.settings import checkUrl, TIMEOUT


def check_network_speed(url=checkUrl):
    try:
        start_time = time.time()
        response = requests.get(url, timeout=TIMEOUT)
        end_time = time.time()

        if response.status_code == 200:
            response_time = end_time - start_time
            if response_time < 0.5:
                print("您目前网络状况良好，项目飞速运行中...")
            else:
                print("您目前网络状况不稳定，请耐心等待项目运行......")
        else:
            print(f"无法访问 {url} 网站，状态码: {response.status_code}")
            print("网络连接失败，请检查您的网络设置。")

    except requests.exceptions.RequestException as e:
        print(f"网络请求失败: {e}")
        print("网络连接失败，请检查您的网络设置。")


def init():
    print("环境检测中")
    check_network_speed()
